//package com.maoshi.shop.scoremall.api.listener;
//
//import cn.hutool.core.date.DateUtil;
//import cn.hutool.core.util.StrUtil;
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.maoshi.shop.bean.dto.OrderRefundDto;
//import com.maoshi.shop.bean.enums.OrderType;
//import com.maoshi.shop.bean.event.OrderRefundSuccessEvent;
//import com.maoshi.shop.bean.model.Order;
//import com.maoshi.shop.bean.model.User;
//import com.maoshi.shop.common.config.Constant;
//import com.maoshi.shop.common.util.Arith;
//import com.maoshi.shop.dao.OrderSettlementMapper;
//import com.maoshi.shop.dao.UserMapper;
//import com.maoshi.shop.service.OrderService;
//import com.maoshi.shop.user.comment.dao.UserLevelMapper;
//import com.maoshi.shop.user.comment.model.UserGrowthLog;
//import com.maoshi.shop.user.comment.service.UserGrowthLogService;
//import lombok.AllArgsConstructor;
//import org.apache.commons.collections.CollectionUtils;
//import org.springframework.context.event.EventListener;
//import org.springframework.stereotype.Component;
//
//import java.util.List;
//import java.util.Objects;
//
///**
// * 退款积分订单监听
// */
//@Component("orderRefundListener")
//@AllArgsConstructor
//public class OrderRefundListener {
//
//    private final OrderService orderService;
//    private final OrderSettlementMapper orderSettlementMapper;
//    private final UserGrowthLogService userGrowthLogService;
//    private final UserMapper userMapper;
//    private final UserLevelMapper userLevelMapper;
//
//    @EventListener(OrderRefundSuccessEvent.class)
//    public void orderRefundSuccessEvent(OrderRefundSuccessEvent event) {
//        OrderRefundDto orderRefundDto = event.getOrderRefundDto();
//        if (CollectionUtils.isEmpty(orderRefundDto.getOrderItems())) {
//            return;
//        }
//        String payNo = orderRefundDto.getOrderPayNo();
//        Order order = orderService.getOne(new LambdaQueryWrapper<Order>().eq(Order::getOrderNumber, orderRefundDto.getOrderNumber()));
//        if (order == null || Objects.equals(order.getOrderType(), OrderType.SCORE.value()) || Objects.isNull(order.getPlatformAmount()) ) {
//            return ;
//        }
//        //退款操作，扣除此笔订单的成长值
//        double totalAmount = orderSettlementMapper.getSumAmountByNum(orderRefundDto.getOrderNumber());
//        //获取此订单历史记录
//        List<UserGrowthLog> userGrowthLogs = userGrowthLogService.list(new LambdaQueryWrapper<UserGrowthLog>().eq(
//                UserGrowthLog::getPayNo, payNo).orderByAsc(UserGrowthLog::getCreateTime));
//        if(CollectionUtils.isEmpty(userGrowthLogs)){
//            return ;
//        }
//        //获取当时增加的成长值
//        UserGrowthLog growthLog = userGrowthLogs.get(0);
//        //获取此笔订单用户剩余增加的成长值
//        long surplusGrowth = 0;
//        for (int i = 1;i < userGrowthLogs.size();i++) {
//            surplusGrowth += userGrowthLogs.get(i).getGrowth();
//        }
//        surplusGrowth = growthLog.getGrowth() - surplusGrowth;
//        if(surplusGrowth == 0){
//            return ;
//        }
//        //获取此笔退款订单需要减少的成长值  当时增加的成长值 * (当前退款订单金额/此笔订单总金额)
//        long mulGrowth = new Double(Arith.mul(growthLog.getGrowth(), Arith.div(orderRefundDto.getOrderAmount(), totalAmount))).longValue();
//        //减少会员成长值
//        User user = userMapper.selectById(growthLog.getUserId());
//
//        //判断如果大于剩余增加的成长值，即扣除当时增加的成长值
//        if(mulGrowth > surplusGrowth){
//            mulGrowth = surplusGrowth;
//        }
//        //修改用户等级
//        long nowGrowth = user.getGrowth() - mulGrowth;
//        List<UserLevelDto> userLevelByGrowh = userLevelMapper.getUserLevelByGrowh(nowGrowth);
//        if(CollectionUtils.isEmpty(userLevelByGrowh)){
//            user.setLevel(Constant.USER_LEVEL_INIT);
//        }else{
//            user.setLevel(userLevelByGrowh.get(0).getLevel());
//        }
//        //修改会员成长值
//        user.setGrowth(nowGrowth);
//        userMapper.updateById(user);
//        //插入会员成长值日志
//        UserGrowthLog userGrowthLog = new UserGrowthLog();
//        userGrowthLog.setCreateTime(DateUtil.date());
//        userGrowthLog.setGrowth(mulGrowth);
//        userGrowthLog.setGrowthAfter(user.getGrowth());
//        userGrowthLog.setRemarks("退款减少成长值");
//        userGrowthLog.setUserId(growthLog.getUserId());
//        userGrowthLog.setPayNo(payNo);
//        userGrowthLogService.save(userGrowthLog);
//    }
//}
